package com.winning.service;

import com.alibaba.fastjson.JSONObject;
import com.winning.dao.CollectDao;
import com.winning.entity.RequestSet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 综合管理模块
 *
 * @author z_dd@winning.com.cn
 * @date 2019/1/23 10:28
 */
@Service
@Slf4j
public class ComprehensiveService {
    /**
     * 行政区划对应表名
     */
    private static final String TABLENAME_XZQH = "MID_XZQH";
    /**
     * 医疗机构对应表名
     */
    private static final String TABLENAME_YLJG = "MID_YLJG";

    /**
     * 机构用户对应表名
     */
    private static final String TABLENAME_JGYH = "MID_JGYH";

    /**
     * 基础数据字典类别对应表名
     */
    private static final String TABLENAME_ZDLB = "MID_JCSJZDLB";

    /**
     * 基础数据字典对应表名
     */
    private static final String TABLENAME_SJZD = "MID_JCSJZD";

    /**
     * 机构科室信息对应表名
     */
    private static final String TABLENAME_JGKS = "MID_JGKSXX";

    /**
     * 机构库位信息对应表名
     */
    private static final String TABLENAME_JGKW = "MID_JGKWXX";

    /**
     * 疾病目录信息对应表名
     */
    private static final String TABLENAME_JBML = "MID_JBML";

    /**
     * 药品单位信息对应表名
     */
    private static final String TABLENAME_YPDW = "MID_YPDW";

    /**
     * 药品给药途径编码信息对应表名
     */
    private static final String TABLENAME_JYTJBM = "MID_YPJYTJBM";

    /**
     * 剂型分类码信息对应表名
     */
    private static final String TABLENAME_JXFLM = "MID_JXFLM";

    /**
     * 药品频次编码信息对应表名
     */
    private static final String TABLENAME_YPPCBM = "MID_YPPCBM";

    /**
     * 医保药品分类码信息对应表名
     */
    private static final String TABLENAME_YBYPFLM = "MID_YBYPFLM";

    /**
     * 生产企业编码信息对应表名
     */
    private static final String TABLENAME_SCQYBM = "MID_SCQYBM";

    /**
     * 药品药理功效分类信息对应表名
     */
    private static final String TABLENAME_YPYLGXFL = "MID_YPYLGXFL";

    /**
     * 方剂来源码信息对应表名
     */
    private static final String TABLENAME_FJLYM = "MID_FJLYM";

    /**
     * 药品基本信息对应表名
     */
    private static final String TABLENAME_YPJBXX = "MID_YPJBXX";

    /**
     * 药品基本信息对应表名
     */
    private static final String TABLENAME_ZCYJBXX = "MID_ZCYJBXX";

    /**
     * 药品品种信息对应表名
     */
    private static final String TABLENAME_YPPZ = "MID_YPPZ";

    /**
     * 药品品规信息对应表名
     */
    private static final String TABLENAME_YPPG = "MID_YPPG";


    private static final String TABLENAME_HCXX = "MID_HCJBXX";


    @Autowired
    RequestSet requestSet;

    @Autowired
    CollectDao collectDao;

    @Autowired
    ResultService resultService;


    /**
     * 行政区划信息接口下载
     *
     * @author Administrator
     * @date 2019/1/28 12:20
     */
    public void xzqhDownload() {
        //获取最大更新时间
        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_XZQH);

        //拼接请求参数
        this.requestSet
                .setInterfaceType("JC3_JK01")
                .setActionCode("JC3_JK01_A01")
                .setRequestParams(new JSONObject().fluentPut("xgsj", maxUploadDate == null ? null : maxUploadDate.toLocaleString()))
                .setRequestTime(new Date());
        resultService.operation(requestSet, TABLENAME_XZQH);
    }

    /**
     * 医疗机构信息接口下载
     *
     * @author Administrator
     * @date 2019/1/28 12:42
     */
    public void yljgDownload() {
        //获取最大更新时间
        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_YLJG);

        //拼接请求参数
        this.requestSet
                .setInterfaceType("JC3_JK01")
                .setActionCode("JC3_JK01_B01")
                .setRequestParams(new JSONObject().fluentPut("xgsj", maxUploadDate == null ? null : maxUploadDate.toLocaleString()).fluentPut("areaCode", "0"))
                .setRequestTime(new Date());
        resultService.operation(requestSet, TABLENAME_YLJG);
    }

    /**
     * 机构用户信息接口下载
     *
     * @author Administrator
     * @date 2019/1/28 16:12
     */
    public void jgyhDownload() {
        //获取最大更新时间
        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_JGYH);
        //拼接请求参数
        this.requestSet
                .setInterfaceType("JC3_JK01")
                .setActionCode("JC3_JK01_C03")
                .setRequestParams(new JSONObject().fluentPut("xgsj", maxUploadDate == null ? null : maxUploadDate.toLocaleString()).fluentPut("addresscode", "0"))
                .setRequestTime(new Date());
        resultService.operation(requestSet, TABLENAME_JGYH);
    }

    /**
     * 基础数据字典接口下载
     * 未完成:没有增量时间
     *
     * @author z_dd@winning.com.cn
     * @date 2019/1/30 12:00
     */
    public void sjzdDownload() {
        //获取最大更新时间
//        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_SJZD);
        //拼接请求参数
        this.requestSet
                .setInterfaceType("JC3_JK01")
                .setActionCode("JC3_JK01_D06")
                .setRequestParams(new JSONObject())
                .setRequestTime(new Date());
        resultService.operation(requestSet, TABLENAME_ZDLB, TABLENAME_SJZD);
    }

    /**
     * 机构科室信息接口下载
     * 未完成: 1.没有增量时间
     * 2.入参"机构编码"不明确
     *
     * @author z_dd@winning.com.cn
     * @date 2019/2/13 10:30
     */
    public void jgksDownload() {
        //获取最大更新时间
//        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_JGKS);
        List<String> orgCodes = collectDao.getOrgCode();
        for (int i = 0; i < orgCodes.size(); i++) {
            this.requestSet
                    .setInterfaceType("JC3_JK01")
                    .setActionCode("JC3_JK01_E03")
                    .setRequestParams(new JSONObject().fluentPut("orgCode", orgCodes.get(i)))
                    .setRequestTime(new Date());
            resultService.operation(requestSet, TABLENAME_JGKS);
        }

    }

    /**
     * 机构库位信息接口下载
     * 未完成: 1.没有增量时间
     *
     * @author z_dd@winning.com.cn
     * @date 2019/2/13 10:49
     */
    public void jgkwDownload() {
        List<String> orgCodes = collectDao.getOrgCode();
        for (int i = 0; i < orgCodes.size(); i++) {
            System.out.println("i=" + i);
            this.requestSet
                    .setInterfaceType("JC3_JK01")
                    .setActionCode("JC3_JK01_F03")
                    .setRequestParams(new JSONObject().fluentPut("orgCode", orgCodes.get(i)))
                    .setRequestTime(new Date());
            resultService.operation(requestSet, TABLENAME_JGKW);
        }
    }

    /**
     * 疾病目录信息接口下载
     * ICD10_PARENTID字段必填返回数据却有NULL
     *
     * @author z_dd@winning.com.cn
     * @date 2019/2/13 10:51
     */
    public void jbmlDownload() {
        //获取最大更新时间
        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_JBML);

        this.requestSet
                .setInterfaceType("JC3_JK01")
                .setActionCode("JC3_JK01_G03")
                .setRequestParams(new JSONObject().fluentPut("xgsj", maxUploadDate == null ? null : maxUploadDate.toLocaleString()))
                .setRequestTime(new Date());
        resultService.operation(requestSet, TABLENAME_JBML);
    }

    /**
     * 药品单位信息接口下载
     *
     * @author z_dd@winning.com.cn
     * @date 2019/2/13 13:12
     */
    public void ypdwDownload() {
        //获取最大更新时间
        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_YPDW);
        this.requestSet
                .setInterfaceType("JC3_JK01")
                .setActionCode("JC3_JK01_H03")
                .setRequestParams(new JSONObject().fluentPut("xgsj", maxUploadDate == null ? null : maxUploadDate.toLocaleString()))
                .setRequestTime(new Date());
        resultService.operation(requestSet, TABLENAME_YPDW);
    }

    /**
     * 给药途径编码信息接口下载
     *
     * @author z_dd@winning.com.cn
     * @date 2019/2/13 13:45
     */
    public void jytjbmDownload() {
        //获取最大更新时间
        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_JYTJBM);

        this.requestSet
                .setInterfaceType("JC3_JK01")
                .setActionCode("JC3_JK01_I03")
                .setRequestParams(new JSONObject().fluentPut("xgsj", maxUploadDate == null ? null : maxUploadDate.toLocaleString()))
                .setRequestTime(new Date());
        resultService.operation(requestSet, TABLENAME_JYTJBM);
    }

    /**
     * 剂型分类码信息接口下载
     *
     * @author z_dd@winning.com.cn
     * @date 2019/2/13 16:49
     */
    public void jxflmDownload() {
        //获取最大更新时间
        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_JXFLM);

        this.requestSet
                .setInterfaceType("JC3_JK01")
                .setActionCode("JC3_JK01_J03")
                .setRequestParams(new JSONObject().fluentPut("xgsj", maxUploadDate == null ? null : maxUploadDate.toLocaleString()))
                .setRequestTime(new Date());
        resultService.operation(requestSet, TABLENAME_JXFLM);
    }

    /**
     * 药品频次编码信息接口下载
     *
     * @author z_dd@winning.com.cn
     * @date 2019/2/14 8:45
     */
    public void yppcbmDownload() {
        //获取最大更新时间
        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_YPPCBM);

        this.requestSet
                .setInterfaceType("JC3_JK01")
                .setActionCode("JC3_JK01_K03")
                .setRequestParams(new JSONObject().fluentPut("xgsj", maxUploadDate == null ? null : maxUploadDate.toLocaleString()))
                .setRequestTime(new Date());
        resultService.operation(requestSet, TABLENAME_YPPCBM);
    }

    /**
     * 医保药品分类码信息接口下载
     *
     * @author z_dd@winning.com.cn
     * @date 2019/2/14 10:20
     */
    public void ybypflmDownload() {
        //获取最大更新时间
        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_YBYPFLM);

        this.requestSet
                .setInterfaceType("JC3_JK01")
                .setActionCode("JC3_JK01_L03")
                .setRequestParams(new JSONObject().fluentPut("xgsj", maxUploadDate == null ? null : maxUploadDate.toLocaleString()))
                .setRequestTime(new Date());
        resultService.operation(requestSet, TABLENAME_YBYPFLM);
    }

    /**
     * 生产企业编码信息接口下载
     *
     * @author z_dd@winning.com.cn
     * @date 2019/2/14 10:29
     */
    public void scqybmDownload() {
        //获取最大更新时间
        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_SCQYBM);

        this.requestSet
                .setInterfaceType("JC3_JK01")
                .setActionCode("JC3_JK01_M03")
                .setRequestParams(new JSONObject().fluentPut("xgsj", maxUploadDate == null ? null : maxUploadDate.toLocaleString()))
                .setRequestTime(new Date());
        resultService.operation(requestSet, TABLENAME_SCQYBM);
    }


    /**
     * 药品药理功效分类信息接口下载
     *
     * @author z_dd@winning.com.cn
     * @date 2019/2/14 11:02
     */
    public void ypylgxflDownload() {
        //获取最大更新时间
        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_YPYLGXFL);

        this.requestSet
                .setInterfaceType("JC3_JK01")
                .setActionCode("JC3_JK01_N03")
                .setRequestParams(new JSONObject().fluentPut("xgsj", maxUploadDate == null ? null : maxUploadDate.toLocaleString()))
                .setRequestTime(new Date());
        resultService.operation(requestSet, TABLENAME_YPYLGXFL);
    }

    /**
     * 方剂来源码信息接口下载
     *
     * @author z_dd@winning.com.cn
     * @date 2019/2/14 11:20
     */
    public void fjlymDownload() {
        //获取最大更新时间
        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_FJLYM);

        this.requestSet
                .setInterfaceType("JC3_JK01")
                .setActionCode("JC3_JK01_O03")
                .setRequestParams(new JSONObject().fluentPut("xgsj", maxUploadDate == null ? null : maxUploadDate.toLocaleString()))
                .setRequestTime(new Date());
        resultService.operation(requestSet, TABLENAME_FJLYM);
    }

    /**
     * 药品基本信息接口下载
     *
     * @author z_dd@winning.com.cn
     * @date 2019/2/14 11:34
     */
    public void ypjbxxDownload() {
        //获取最大更新时间
        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_YPJBXX);

        this.requestSet
                .setInterfaceType("JC3_JK01")
                .setActionCode("JC3_JK01_P03")
                .setRequestParams(new JSONObject().fluentPut("xgsj", maxUploadDate == null ? null : maxUploadDate.toLocaleString()))
                .setRequestTime(new Date());
        resultService.operation(requestSet, TABLENAME_YPJBXX);
    }

    /**
     * 中草药基本信息接口下载
     *
     * @author z_dd@winning.com.cn
     * @date 2019/2/14 14:26
     */
    public void zcyjbxxDownload() {
        //获取最大更新时间
        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_ZCYJBXX);

        this.requestSet
                .setInterfaceType("JC3_JK01")
                .setActionCode("JC3_JK01_Q03")
                .setRequestParams(new JSONObject().fluentPut("xgsj", maxUploadDate == null ? null : maxUploadDate.toLocaleString()))
                .setRequestTime(new Date());
        resultService.operation(requestSet, TABLENAME_ZCYJBXX);
    }

    /**
     * 药品品种信息接口下载
     *
     * @author z_dd@winning.com.cn
     * @date 2019/2/14 14:36
     */
    public void yppzDownload() {
        //获取最大更新时间
        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_YPPZ);

        this.requestSet
                .setInterfaceType("JC3_JK01")
                .setActionCode("JC3_JK01_R03")
                .setRequestParams(new JSONObject().fluentPut("xgsj", maxUploadDate == null ? null : maxUploadDate.toLocaleString()))
                .setRequestTime(new Date());
        resultService.operation(requestSet, TABLENAME_YPPZ);
    }

    /**
     * 药品品规信息接口下载
     *
     * @author z_dd@winning.com.cn
     * @date 2019/2/14 14:59
     */
    public void yppgDownload() {
        //获取最大更新时间
        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_YPPG);

        this.requestSet
                .setInterfaceType("JC3_JK01")
                .setActionCode("JC3_JK01_S03")
                .setRequestParams(new JSONObject().fluentPut("xgsj", maxUploadDate == null ? null : maxUploadDate.toLocaleString()))
                .setRequestTime(new Date());
        resultService.operation(requestSet, TABLENAME_YPPG);
    }

    /**
     * 耗材基本信息接口下载
     *
     * @author z_dd@winning.com.cn
     * @date 2019/2/15 9:44
     */
    public void hcxxDownload() {
        //获取最大更新时间
        Date maxUploadDate = collectDao.getMaxUploadDate(TABLENAME_HCXX);

        this.requestSet
                .setInterfaceType("JC3_JK01")
                .setActionCode("JC3_JK01_T03")
//                .setRequestParams(new JSONObject().fluentPut("xgsj", maxUploadDate == null ? null : maxUploadDate.toLocaleString()))
                .setRequestParams(new JSONObject().fluentPut("xgsj", "2018-02-15 01:02:03"))
                .setRequestTime(new Date());
        resultService.operation(requestSet, TABLENAME_HCXX);
    }
}
